package com.hsjk.szwj.module.train.controller.app;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.security.core.annotations.PreAuthenticated;
import com.hsjk.szwj.module.train.controller.app.vo.order.OrderBasicInfoRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.order.OrderCancelReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.order.OrderCreateReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.order.OrderInfoRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.order.OrderListRespVO;
import com.hsjk.szwj.module.train.service.IOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/28 10:41
 **/
@Tag(name = "应用端 - 订单相关接口")
@RestController
@RequestMapping("/train/order")
@Validated
public class AppOrderController {

    @Resource
    private IOrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    @PreAuthenticated
    public CommonResult<Long> createOrder(@RequestBody
                                          @Valid OrderCreateReqVO reqVO) {
        return success(orderService.createOrder(reqVO));
    }

    @GetMapping("/get-by-id")
    @Operation(summary = "根据订单id获取订单信息")
    @PreAuthenticated
    public CommonResult<OrderInfoRespVO> getOrderInfoById(@RequestParam("id") Long id) {
        return success(orderService.getOrderInfoById(id));
    }

    @GetMapping("/get-by-order-no")
    @Operation(summary = "根据订单号获取订单信息")
    @PreAuthenticated
    public CommonResult<OrderInfoRespVO> getOrderInfoByOrderNo(@RequestParam("orderNo") String orderNo) {
        return success(orderService.getOrderInfoByOrderNo(orderNo));
    }

    @GetMapping("/get-basic-info-by-pay-order-id")
    @Operation(summary = "根据订单号获取订单基础信息")
    public CommonResult<OrderBasicInfoRespVO> getBasicInfoByPayOrderId(@RequestParam("payOrderId") String payOrderId) {
        return success(orderService.getOrderBasicInfoByPayOrderId(payOrderId));
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消订单")
    @PreAuthenticated
    public CommonResult<Boolean> cancelOrder(@RequestBody
                                             @Valid OrderCancelReqVO reqVO) {
        return success(orderService.cancelOrder(reqVO.getId()));
    }

    @GetMapping("/list-member-order")
    @Operation(summary = "获取用户订单列表")
    @PreAuthenticated
    public CommonResult<List<OrderListRespVO>> getOrderListByMemberId() {
        return success(orderService.getOrderListByMemberId());
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单")
    @PreAuthenticated
    public CommonResult<Boolean> deleteRole(@RequestParam("id") Long id) {
        orderService.deleteOrder(id);
        return success(true);
    }
}
